package com.spiderette.ace.sdk.ad;

import android.app.Activity;
import android.content.Context;
import android.util.Log;

import androidx.annotation.NonNull;

import com.applovin.mediation.MaxAd;
import com.applovin.mediation.MaxError;
import com.applovin.mediation.MaxReward;
import com.applovin.mediation.MaxRewardedAdListener;
import com.applovin.mediation.ads.MaxRewardedAd;
import com.spiderette.ace.sdk.Constant;


public class SAceMaxReward implements SAceVideoIAP {
    private static SAceMaxReward adManager;
    private MaxRewardedAd maxRewardedAd;

    private VideoRewardCallback mlistner;

    public static SAceMaxReward getInstance() {
        if (adManager == null) {
            adManager = new SAceMaxReward();
        }
        return adManager;
    }

    private MaxRewardedAd getRewardedAd(Context context,String adUnitId) {
        return MaxRewardedAd.getInstance(adUnitId, context);
    }

    public void loadRewardAd(Context context) {
        maxRewardedAd =getRewardedAd(context, Constant.REWARD_PLACEMENTID);
        mlistner=null;
        setListner();
        loadRewardAd();
    }

    private void loadRewardAd(){
        maxRewardedAd.loadAd();
        Log.e("SAce","load max");
    }


    private void setListner() {
        maxRewardedAd.setListener(new AdListener());
    }

    @Override
    public boolean isReady(){
        if (maxRewardedAd != null) {
            return maxRewardedAd.isReady();
        }
        return false;
    }

    @Override
    public void showVideo(Activity activity, VideoRewardCallback listner) {
        if (maxRewardedAd == null) {
            maxRewardedAd =getRewardedAd(activity,Constant.REWARD_PLACEMENTID);
        }
        mlistner=listner;
        setListner();
        if (isReady()) {
            maxRewardedAd.showAd();
        } else {
            loadRewardAd();
            if (listner!=null){
                listner.onAdError();
            }
        }
    }

    public class AdListener implements MaxRewardedAdListener{

        @Override
        public void onUserRewarded(@NonNull MaxAd maxAd, @NonNull MaxReward maxReward) {

        }

        @Override
        public void onAdLoaded(@NonNull MaxAd maxAd) {
            Log.e("SAce","onAdLoaded");
        }

        @Override
        public void onAdDisplayed(@NonNull MaxAd maxAd) {
            Log.e("SAce","onAdDisplayed"+maxAd.getNetworkName());
        }

        @Override
        public void onAdHidden(@NonNull MaxAd maxAd) {
            loadRewardAd();
            if (mlistner != null) {
                mlistner.onAdOver();
            }
        }

        @Override
        public void onAdClicked(@NonNull MaxAd maxAd) {

        }

        @Override
        public void onAdLoadFailed(@NonNull String s, @NonNull MaxError error) {
            Log.d("SAce","onMaxRewardLoadFailed "+error.getCode()+ error.getMessage()+error.getMediatedNetworkErrorCode()+error.getMediatedNetworkErrorMessage());
        }

        @Override
        public void onAdDisplayFailed(@NonNull MaxAd maxAd, @NonNull MaxError error) {
            Log.e("SAce","onAdDisplayFailed "+error.getCode() +error.getMessage()+error.getMediatedNetworkErrorCode()+error.getMediatedNetworkErrorMessage());
            if (mlistner != null) {
                mlistner.onAdError();
            }
        }
    }
}
